/*
 * Authors: Filipe Ferraz Salgado  (filipesalgado@hotmail.com)
 *          Alexandre Albano       (aalbano@gmail.com)
 *          Instituto de Matemática e Estatística
 *          Universidade de São Paulo
 */

package br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.termvector.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.Tag;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.TagMagnitude;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.TagMagnitudeMatrix;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.TagMagnitudeVector;

public class TagMagnitudeMatrixImpl implements TagMagnitudeMatrix {
    private List<TagMagnitudeVector> vectorList;
    private Map<Tag, TagMagnitude> uniqueTags;
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < vectorList.size(); i++) {
	        TagMagnitudeVector tmv = vectorList.get(i);
            sb.append("[" + i + ",size=" + tmv.getTagMagnitudeMap().size() + "]: " + tmv);
            sb.append("\n");
        }
        return sb.toString();
    }
   
	public List<TagMagnitudeVector> getVectorList() {
		return vectorList;
	}

	public void addToVectorList(TagMagnitudeVector vector) {
		vectorList.add(vector);
		uniqueTags.putAll(vector.getTagMagnitudeMap());
		
		for(Tag tag : uniqueTags.keySet()) {
			for(TagMagnitudeVector tmv : vectorList) {
				if (tmv.getTagMagnitudeMap().containsKey(tag) == false) {
					tmv.getTagMagnitudeMap().put(tag, new TagMagnitudeImpl(tag, 0.0));
				}
			}
		}
	}

	public TagMagnitudeMatrixImpl() {
		vectorList = new LinkedList<TagMagnitudeVector>();
		uniqueTags = new HashMap<Tag, TagMagnitude>();
	}
	
	@Override
	public List<TagMagnitudeVector> getTagMagnitudeVectors() {
		return vectorList;
	}
	
	public Double[][] getSimpleRepresentation() {
		Double[][] ret = new Double[vectorList.size()][uniqueTags.size()];

        int i = 0;
        for(Tag tag: uniqueTags.keySet()) {
            for(int j = 0; j < vectorList.size(); j++) {
            	TagMagnitudeVector tmv = vectorList.get(j);
            	ret[j][i] = tmv.getTagMagnitudeMap().get(tag).getMagnitude();
            }
            i++;
        }

        return ret;
	}

	public Map<Tag, TagMagnitude> getUniqueTags() {
		return uniqueTags;
	}
	
	public List<String> getStemmedTags() {
		ArrayList<String> ret = new ArrayList<String>();
		Set<Tag> tags = uniqueTags.keySet();
		
		for(Tag tag : tags) {
			ret.add(tag.getStemmedText());
			
		}
		return ret;
	}
	
	public List<String> getDisplayTextTags() {
		ArrayList<String> ret = new ArrayList<String>();
		Set<Tag> tags = uniqueTags.keySet();
		
		for(Tag tag : tags) {
			ret.add(tag.getDisplayText());
			
		}
		return ret;
	}

}
